<?php 

/* open_labs.php -- Connects to the MySQL database, retrieves information from the hours_workes table,
which stores information about the shift. Every shift in the table has a start_time, but not an end_time;
in fact, if a shift does not have an end_time, it means the TC is currently clocked in, and the lab into
which the TC is clocked is presumably staffed. We find out the lab_id of each of these shifts, store the 
values in an array, and then compare those against the list of lab names, which is how we find out which
labs are currently staffed. There are a number of problems with this...

	- A TC doesn't always clock into the right lab
	- A TC may not clock in
	- A TC may forget to clock out

Hopefully we can find some creative ways of dealing with these problems effectively. */

// Include important constants and set admin_home

require_once("init.php");
$admin_home = ADMIN_HOME;

// Include TCDB 'init.php' to Make a MySQL Connection

include("../tcdb/html/init.php");

// We want yesterday's date, since a shift that starts one day may not end on that day (a la MathLAN and Gencomp)

$date = date("Y-m-d", time() - 86400);

// Retrieve data from DB

$result = mysql_query("SELECT date, start_time, end_time, lab_id 
		       FROM hours_worked 
                       WHERE date >= '$date'
		       AND ISNULL(end_time)") or die(mysql_error());

// Create an array of lab id's...

$array_of_lab_ids = array();

// And store the data into that array

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$array_of_lab_ids[] = $row['lab_id'];
	}

// For testing purposes, remove the comments below

// print_r ($array_of_lab_ids);

// Now, create an associative array of lab names and lab id's

$lab_names_and_ids = array(
'helpdesk1' => '6', 
'helpdesk2' => '7', 
'gencomp' => '1', 
'av1' => '18', 
'av2' => '19', 
'ccl' => '36', 
'scilab' => '37', 
'mathlan' => '3', 
'iif' => '4', 
'burling' => '21');

/* And, create a set of $lab_open variables, all of which are set to FALSE to 
begin with; the labs are closed if they are not open, or guilty until proven innocent... */

foreach($lab_names_and_ids as $lab => $lab_id) {
	${"{$lab}_open"} = FALSE;
	}

// Then loop through the arrays to see which labs should be set open

foreach($array_of_lab_ids as $lab_id) {
	foreach($lab_names_and_ids as $lab => $other_lab_id) {
		if ($lab_id == $other_lab_id) {
			${"{$lab}_open"} = TRUE;
			}
		}
	}

// We need to do just a bit more...

// If either a Helpdesk 1 shift or a Helpdesk 2 shift is staffed, the Helpdesk is open

if ($helpdesk1_open || $helpdesk2_open) {
	$helpdesk_open = TRUE;
	}

// The same follows for the AV Center

if ($av1_open || $av2_open) {
	$av_open = TRUE;
	}

// If the Helpdesk is open, the CCL is also open

if ($helpdesk_open) {
	$ccl_open = TRUE;
	}

/* The following code defines lab openings based on a constant set of hours... some labs are
always open, while others are open at certain times of day regardless of whether or not a TC 
is on duty */

/* Lazier, Read, Cleveland, Younker, Noyce 2402/2403 and Norris are always open, unless it's a holiday... 
I'll figure out a good way to find that out dynamically... what is something that is on only while
school is in session? */

// We should also figure out a way to be able to set these labs as closed, NOT ONLY in bulk as we do here

include("init.php");

if (!(HOLIDAY)) {
	$lazier_open = TRUE;
	$read_open = TRUE;
	$cleveland_open = TRUE;
	$younker_open = TRUE;
	$noyce_open = TRUE;
	$norris_open = TRUE;
	}
else {
	$lazier_open = FALSE;
	$read_open = FALSE;
	$cleveland_open = FALSE;
	$younker_open = FALSE;
	$noyce_open = FALSE;
	$norris_open = FALSE;
	}

// Now, define your variables based on time/date...

$day = date ( 'l' );
$hour = date ( 'H' );

// And check to see if the other labs are open... I'm not sure if this is the best way to accomplish this

// Gencomp, Inslab, and Scilab should be open from 8:00-17:00 Monday-Friday

if ((($day == 'Monday' || $day == 'Tuesday' || $day == 'Wednesday' ||
      $day == 'Thursday' || $day == 'Friday') && $hour >= 8 && $hour <= 16)) {
	$gencomp_open = TRUE;
	$scilab_open = TRUE;
	}

// MathLAN is open for most of the day (what about the weekend?)

if ((($day == 'Monday' || $day == 'Tuesday' || $day == 'Wednesday' ||
      $day == 'Thursday') && $hour >= 8 && $hour <= 23) ||
    (($day == 'Tuesday' || $day == 'Wednesday' ||
      $day == 'Thursday' || $day == 'Friday') && $hour == 0)) {
	$mathlan_open = TRUE;
	}

// The Science Library is also open for most of the day...

if ((($day == 'Monday' || $day == 'Tuesday' || $day == 'Wednesday' || $day == 'Thursday') && $hour >= 8 && $hour <= 23) ||
     ($day == 'Friday' && $hour >= 8 && $hour < 17) ||
     ($day == 'Saturday' && $hour >= 13 && $hour < 17) ||
     ($day == 'Sunday' && $hour >= 13 && $hour <= 23)) {
	$scilib_open = TRUE;
	}

// What about the IIF?


// Burling Library hours

if ((($day == 'Monday' || $day == 'Tuesday' || $day == 'Wednesday' || $day == 'Thursday') && $hour >= 8 && $hour <= 23) || (($day == 'Monday' || $day == 'Tuesday' || $day == 'Wednesday' || $day == 'Thursday' || $day == 'Friday') && $hour == 0) ||
     ($day == 'Friday' && $hour >= 8 && $hour < 22) ||
     ($day == 'Saturday' && $hour >= 10 && $hour < 22) ||
     ($day == 'Sunday' && $hour >= 10 && $hour <= 23)) {
	$burling_open = TRUE;
	}

/* Now, we want to also write the printer status to file. First we need to create an array of lab names,
then loop through them to figure out which labs are open and which are closed. Why have both open_status
variables and files? For one, shell scripts can't access these php variables, and we need to be able to have
our shell scripts access this information so that we're keeping appropriate statistics. */

$lab_name_array = array( helpdesk, av, ccl, lazier, read, cleveland, younker, noyce, norris,
	            gencomp, scilab, mathlan, scilib, burling, iif );

foreach ($lab_name_array as $lab) {

	// If a lab is open, a "1" should be written to file

	if (${"{$lab}_open"}) {
		$fp = fopen ("$admin_home/labs/open_statuses/$lab", 'w');
		fwrite ($fp, "1\n");
		fclose($fp);
	}
	
	// Otherwise, a "0" should be written to file

	else {
		$fp = fopen ("$admin_home/labs/open_statuses/$lab", 'w');
		fwrite ($fp, "0\n");
		fclose($fp);
	}
}

/* * * * * * * * * * * * 
* PRINTER OPEN STATUSES *
* * * * * * * * * * * */

$printer_names = array( baskerville, cleveland, lib_iif, lib_1st_flr, lib_2nd_flr, norris2, read2, av1, lazier,
		       inslab2, scilab, scilib, jrc_email, younker2, helpdesk2, duerer, gencomp2, rose, ccl3_bw, escher );

foreach($printer_names as $printer) {
	${"{$printer}_printer_open"} = FALSE;
	}

// Just as with the labs, the corresponding dorm printers should also always be available

if (!(HOLIDAY)) {
	$baskerville_printer_open = TRUE;
	$cleveland_printer_open = TRUE;
	$norris2_printer_open = TRUE;
	$read2_printer_open = TRUE;
	$jrc_email_printer_open = TRUE;
	$younker2_printer_open = TRUE;
	$rose_printer_open = TRUE;
	$lazier_printer_open = TRUE;
	}
else {
	$baskerville_printer_open = FALSE;
	$cleveland_printer_open = FALSE;
	$norris2_printer_open = FALSE;
	$read2_printer_open = FALSE;
	$jrc_email_printer_open = FALSE;
	$younker2_printer_open = FALSE;
	$rose_printer_open = FALSE;
	$lazier_printer_open = FALSE;
	}

// The other printers should only be available if their corresponding labs are also open

if ($helpdesk_open) {
	$helpdesk2_printer_open = TRUE;
	}

if ($ccl_open) {
	$ccl3_bw_printer_open = TRUE;
	}

if ($av_open) {
	$av_printer_open = TRUE;
	}

if ($iif_open) {
	$lib_iif_printer_open = TRUE;
	}

if ($burling_open) {
	$lib_1st_flr_printer_open = TRUE;
	$lib_2nd_flr_printer_open = TRUE;
	}

if ($gencomp_open) {
	$gencomp2_printer_open = TRUE;
	$inslab2_printer_open = TRUE;
	}

if ($scilab_open) {
	$scilab_printer_open = TRUE;
	}

if ($scilib_open) {
	$scilib_printer_open = TRUE;
	}

// DUERER and ESCHER should always be available, but is Noyce open 24/7?

if ($mathlan_open) {
	$duerer_printer_open = TRUE;
	$escher_printer_open = TRUE;
	}

// Write printer open statuses to file...

foreach ($printer_names as $printer) {

	// If a lab is open, a "1" should be written to file

	if (${"{$printer}_printer_open"}) {
		$fp = fopen ("$admin_home/printers/open_statuses/$printer", 'w');
		fwrite ($fp, "1\n");
		fclose($fp);
	}
	
	// Otherwise, a "0" should be written to file

	else {
		$fp = fopen ("$admin_home/printers/open_statuses/$printer", 'w');
		fwrite ($fp, "0\n");
		fclose($fp);
	}
}

?>

